﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using System.Transactions;
using TPOJ.BLL;
using TPOJ.Model;
using TPOJ.Core;
using System.Threading;

namespace JudgeService
{
    public partial class TPOJJudgeService : ServiceBase
    {
        private static int transactionTimeout = int.Parse(ConfigurationManager.AppSettings["TransactionTimeout"]);
        private static string sourcePath = ConfigurationManager.AppSettings["SourcePath"];
        private static string outputPath = ConfigurationManager.AppSettings["OutputPath"];
        private static string testsPath = ConfigurationManager.AppSettings["TestsPath"];
        public TPOJJudgeService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {            
            TimeSpan tsTimeout = TimeSpan.FromSeconds(Convert.ToDouble(transactionTimeout));      
            Message message = null;
            while (true)
            {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tsTimeout))
                {
                    try
                    {
                        message = MessageBO.ReceiveFromQueue(tsTimeout.Milliseconds);
                    }
                    catch(System.TimeoutException)
                    {
                    }
                    ts.Complete();
                }
                if (message != null)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(Judge), message);
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
        }

        protected override void OnStop()
        {
        }

        private static void Judge(object stateInfo)
        {      
            
        }
    }
}
